/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package optimizacion.problemas;

import Distribuciones.Uniforme;
import ec.util.MersenneTwisterFast;

/**
 *
 * @author USUARIO
 */
public class VolumenOptimo {
    public static void main(String[] args) {
        MersenneTwisterFast prng = new MersenneTwisterFast();
        double limite = 21.59D/2D;
        System.out.println(limite);
        Uniforme u1 = new Uniforme(0,limite);
        double fopt = 0D;
        double xopt = 0D;
        double deltaX= limite/1000000D;
        int n = 10000;
        for (int i = 0; i < n; i++) {
            double xi = u1.generar(prng.nextDouble());
            double yi = fx(xi);
            double xi_plus = xi + deltaX;
            double xi_minus = xi - deltaX;
            boolean direccion= true;
            int delta;
            if ((fx(xi_plus) > yi) && (fx(xi_plus) > fx(xi_minus))) {
                direccion = true;
                delta= 1;
            } else if ((fx(xi_minus) > yi) && (fx(xi_plus) < fx(xi_minus))){
                direccion = true;
                delta= -1;
            } else {
                direccion = false;
                delta= 0;
            }
            double xi_opt_i = xi;
            double fi_opt_i = yi;
            
            if (direccion) {
                double deltaO = delta*deltaX;
                while ((fx(xi_opt_i+ deltaO) > fi_opt_i) && (xi_opt_i+ deltaO > 0) && (xi_opt_i+ deltaO < limite)) {
                    xi_opt_i += deltaO;
                    fi_opt_i =fx(xi_opt_i);
                }
            }
            if (fi_opt_i > fopt) {
                fopt = fi_opt_i;
                xopt = xi_opt_i;
            }
            
        }
        System.out.println("x maximo: " + xopt);
        System.out.println("f(x) maximo: "+ fopt);
    }
    
    public static double fx(double x){
        return x*(21.59D - 2*x)*(27.94D - 2*x);
    }
}
